package com.ikas.ai.server.module.steady.controller;

import com.ikas.ai.handle.JsonResult;
import com.ikas.ai.model.PageQuery;
import com.ikas.ai.model.UpdateGroup;
import com.ikas.ai.server.module.steady.model.dto.StyMonMeteQueryDto;
import com.ikas.ai.server.module.steady.model.dto.StyMonMeteSaveDto;
import com.ikas.ai.server.module.steady.model.dto.StyMonMeteUpdateDto;
import com.ikas.ai.server.module.steady.model.po.SteadyMonitorMete;
import com.ikas.ai.server.module.steady.service.SteadyMonitorMeteService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * 稳态监测测点配置管理
 *
 * @author wuyf
 * @since 2023-12-13
 */
@RestController
@RequestMapping("/steadyMonitorMete")
public class SteadyMonitorMeteController {

    @Resource
    private SteadyMonitorMeteService steadyMonitorMeteService;

    /**
     * 分页查询
     *
     * @param queryDto 入参对象
     * @param page     分页信息
     * @return json
     */
    @GetMapping("/page")
    public JsonResult pageStyMonMete(@Validated StyMonMeteQueryDto queryDto, PageQuery page) {
        return JsonResult.ok(steadyMonitorMeteService.pageStyMonMete(queryDto, page));
    }

    /**
     * 列表查询
     *
     * @param queryDto 入参对象
     * @return json
     */
    @GetMapping("/list")
    public JsonResult listStyMonMete(@Validated StyMonMeteQueryDto queryDto) {
        return JsonResult.ok(steadyMonitorMeteService.listStyMonMete(queryDto));
    }

    /**
     * 下拉框
     *
     * @return json
     */
    @GetMapping("/all")
    public JsonResult all() {
        return JsonResult.ok(steadyMonitorMeteService.list().stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(Comparator.comparing(SteadyMonitorMete::getMeteName))), ArrayList::new)
        ));
    }


    /**
     * 新增稳态监视测点
     *
     * @param styMonMeteDto 入参对象
     * @return
     */
    @PostMapping("/add")
    public JsonResult addStyMonMete(@RequestBody @Validated StyMonMeteSaveDto styMonMeteDto) {
        steadyMonitorMeteService.addStyMonMete(styMonMeteDto);
        return JsonResult.ok();
    }

    /**
     * 修改稳态监视测点
     *
     * @param styMonMeteDto 入参对象
     * @return
     */
    @PostMapping("/update")
    public JsonResult updateStyMonMete(@RequestBody @Validated(UpdateGroup.class) StyMonMeteUpdateDto styMonMeteDto) {
        steadyMonitorMeteService.updateStyMonMete(styMonMeteDto);
        return JsonResult.ok();
    }

    /**
     * 明细查询
     *
     * @param id 主键id
     * @return json
     */
    @GetMapping("/detail")
    public JsonResult detailStyMonMete(@RequestParam Long id) {
        return JsonResult.ok(steadyMonitorMeteService.detail(id));
    }

    /**
     * 无效/生效设置
     *
     * @param id 主键id
     * @return json
     */
    @PostMapping("/enable/{id}")
    public JsonResult enableStyMonMete(@PathVariable Long id) {
        steadyMonitorMeteService.enableStyMonMete(id);
        return JsonResult.ok();
    }

    /**
     * 单个删除
     *
     * @param id 主键id
     * @return json
     */
    @GetMapping("/del/{id}")
    public JsonResult delStyMonMete(@PathVariable Long id) {
        steadyMonitorMeteService.delStyMonMete(id);
        return JsonResult.ok();
    }

    /**
     * 批量删除
     *
     * @param ids list of id
     * @return json
     */
    @GetMapping("/delBatch")
    public JsonResult delBatchStyMonMete(@RequestParam("ids") List<Long> ids) {
        steadyMonitorMeteService.delBatchStyMonMete(ids);
        return JsonResult.ok();
    }

    /**
     * 导入监测测点
     *
     * @param file
     * @param index
     * @return
     * @throws Exception
     */
    @PostMapping("/importMete")
    public JsonResult importMete(@RequestParam("file") MultipartFile file, @RequestParam("index") Integer index) throws Exception {
        steadyMonitorMeteService.importMete(file, index);
        return JsonResult.ok();
    }

}
